package com.android.videotest.activity;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import android.widget.VideoView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.android.videotest.R;
import com.android.videotest.Urls;

import java.io.File;

public class VideoViewActivity extends AppCompatActivity {
    private static final String TAG = "VideoViewActivity";

    public static void start(Context context, File file) {
        Intent starter = new Intent(context, VideoViewActivity.class);
        starter.putExtra("file", file);
        context.startActivity(starter);
    }
    public static void start(Context context, String path) {
        Intent starter = new Intent(context, VideoViewActivity.class);
        starter.putExtra("path", path);
        context.startActivity(starter);
    }

    public static void start(Context context) {
        Intent starter = new Intent(context, VideoViewActivity.class);
//    starter.putExtra();
        context.startActivity(starter);
    }

    VideoView videoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_view);
        videoView = findViewById(R.id.video_View);
        String url = Urls.shuangyinqijing;
//        String url = "http://baobab.kaiyanapp.com/api/v1/playUrl?vid=177937&resourceType=video&editionType=default&source=aliyun&playUrlType=url_oss";
////        URL url = new Uri("http://baobab.kaiyanapp.com/api/v1/playUrl?vid=177937&resourceType=video&editionType=default&source=aliyun&playUrlType=url_oss");
////        URI uri = url.toURI();
//       videoView.setVideoPath(url1);
//        File file = (File) getIntent().getSerializableExtra("file");
//        if (file == null) {
//            return;
//        }
        //设置播放资源(可以是应用的资源文件／url／sdcard路径)
//        mMediaPlayer.setDataSource(VideoViewActivity.this, uri);
//        Uri uri = Uri.fromFile(file);
//        videoView.setVideoURI(uri);
//        if (ContextCompat.checkSelfPermission(VideoViewActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
//            ActivityCompat.requestPermissions(VideoViewActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
//            Log.d(TAG, "onCreate: 请求权限");
//        } else {
//
//            Log.d(TAG, "onCreate: 有权限");
//        }

//        VideoController controller = new VideoController(this);
//        controller.setTitle(file.getName());
//        controller.attachToVideoView(videoView);
//        videoView.start();

//        Uri uri = Uri.parse("android.resource://com.android.videotest/" + R.raw.test_vide);
//        videoView.setVideoURI(uri);

        videoView.setVideoPath(url);
        videoView.start();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case 1:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Log.d(TAG, "onRequestPermissionsResult: 已授予权限");
                } else {
                    Toast.makeText(this, "You denied the permission", Toast.LENGTH_SHORT).show();
                    Log.d(TAG, "onRequestPermissionsResult: 禁止权限");
                }
                break;
            default:
        }
    }

//    @Override
//    protected void start() {
//        super.start();
//        videoView.start();
//    }
//
//    @Override
//    protected void stop() {
//        super.stop();
//        videoView.pause();
//    }
//
//    @Override
//    protected void onDestroy() {
//        super.onDestroy();
//        videoView.stopPlayback();
//    }
}
